<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/6/11
 * Time: 11:25
 */

namespace app\account\controller;

use app\account\model\BonusPointManager;
use app\alipay\model\AddressManager;
use app\common\model\Base;
use app\account\model\Getdeposit;

class BonusPoint extends Base{

    const MISS_UID = "请输入用户uid";
    private $bonusPoint;
    public function __construct()
    {
        $this->bonusPoint = new BonusPointManager();
    }

    public function index(){

        $status = empty($_GET["status"])?-1:$_GET["status"];

        $page = empty($_GET["page"])?0:$_GET["page"];
        $result = array(
            "code"=>1,
            "count"=>$this->bonusPoint->getAllBonusPointCountByStatus($status),
            "msg"=>$this->bonusPoint->getAllBonusPointStatusByUid($status,$page)
        );
        if($result){
            echo json_encode($result);
        }else
        {
            echoJson(0, "读取失败");
        }
    }

    public function test(){
        $this->bonusPoint->test($_GET["uid"]);
    }

    /**
     *
     * @api {get} /BonusPoint/create 支付宝-发起积分提取
     * @apiDescription 支付宝-发起积分提取
     * @apiGroup account
     * @apiParam {string} uid 用户的uid
     * @apiParam {string} amount 积分
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "uid": 17754928306，
     *       "amount": "xxx",
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 返回消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "创建积分提取订单成功"
     *     }
     */
    public function create(){
        $uid = $_GET['uid'];
        $bonusPoint = $_GET['amount'];

        $mAddress = new AddressManager();

        if(!$mAddress->getAddress($uid)){
            echoJson(0, "请填写支付宝账号或姓名");
        }

        if(empty($uid)){
            echoJson(0,self::MISS_UID);
        }else if(empty($bonusPoint)){
            echoJson(0, "请输入积分数目");
        }

        if($this->bonusPoint->setBonusPointExtract($uid, $bonusPoint)){
            echoJson(1, "积分提取订单成功");
        }else{
            echoJson(0, "积分提取失败");
        }
    }

    /**
     *
     * @api {put} /BonusPoint/:id 支付宝-积分确认提取
     * @apiDescription 支付宝-积分确认提取
     * @apiGroup account
     * @apiParam {string} :id 积分提取订单号
     *
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 返回消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "积分提取成功"
     *     }
     */
    public function update($id){
        if($this->bonusPoint->confirmBonusPointExtract($id)){
            echoJson(1, "提取成功");
        }
    }

    /**
     *
     * @api {get} /BonusPoint/:id 支付宝-积分订单读取
     * @apiDescription 支付宝-积分订单读取
     * @apiGroup account
     * @apiParam {string} :id 用户的uid
     * @apiParam {string} [status] 积分订单状态 0-发起积分提取 1-提取成功 2-提取失败 10-积分获取
     *
     *@apiParamExample {json} Request-Example:
     *     {
     *       "uid": 17754928306，
     *       "amount": "xxx",
     *     }
     *
     * @apiSuccess {String} code 0 表示失败 1 表示成功.
     * @apiSuccess {String} msg 返回消息.
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": "1",
     *       "msg": "积分提取订单成功"
     *     }
     */
    public function read($id){
        $status = $_GET["status"];
        $page = empty($_GET["page"])?0:$_GET["page"];
        $result = $this->bonusPoint->getBonusPointExtractStatusByUid($id,$status,$page);
        if($result){
            echoJson(1, $result);
        }else
        {
            echoJson(0, "读取失败");
        }
    }
}